<!-- subpkg_consult/department/index.vue -->
<script lang="ts" setup>
  import { departmentListApi } from '@/apis/consult'
  import type { DepartData } from '@/types/consult'
  import { computed, ref } from 'vue'
  import { objectToQueryParams } from '@/utils'

  //科室列表
  const list = ref<DepartData[]>([])
  // 高亮索引
  const activeIndex = ref(0)
  // 获取科室列表
  const getData = async () => {
    const res = await departmentListApi()
    list.value = res
  }
  // 调用获取数据的方法
  getData()
  // 通过高亮索引获取二级科室列表
  const childList = computed(() => {
    // 注意；数据还没请求回来时，没有 .child属性，这里需要用可选链安全访问
    return list.value[activeIndex.value]?.child
  })

  const pageParams = defineProps<{ type: string;illnessType: string }>()
</script>

<template>
  <view class="department-page">
    <scroll-view scroll-y class="department-primary">
      <!-- v-for 和 v-if 不能同时使用，可通过 template 空标签包裹，书写指令  -->
      <template v-for="(item, index) in list" :key="item.id">
        <view
          v-if="item.child.length > 0"
          class="department-item"
          :class="{ active: activeIndex == index }"
          @click="activeIndex = index"
          >{{ item.name }}</view
        >
      </template>
    </scroll-view>
    <scroll-view class="department-secondary">
      <navigator
        v-for="item in childList"
        :key="item.id"
        hover-class="none"
        :url="`/subpkg_consult/description/index?${objectToQueryParams({
          ...pageParams,
          depId: item.id,
        })}`"
        class="department-item"
      >
        {{ item.name }}
      </navigator>
    </scroll-view>
  </view>
</template>

<style lang="scss">
  @import './index.scss';
</style>
